package com.greenant.store.activity;


import java.util.ArrayList;

import android.app.ActionBar;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.greenant.store.R;
import com.greenant.store.fragment.GoodsListFragment;
import com.greenant.store.fragment.MyFragmentPagerAdapter;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;

/**
 * * 
 *
 * 
 * 
 */
public class MyGoodsActivityOld extends FragmentActivity implements OnRefreshListener<ListView> {
	public static final String STYLE_ON_SHELF ="OnShelf";
	public static final String STYLE_OFF_SHELF ="OffShelf";
	public static final String STYLE_GOODS_STYLES ="GetGoodsStyles";
	public static final String STYLE_GOODS_PERSTYLE ="GetGoodsPerStyle";
    private static final String TAG = "MyStoreActivity";
    private ViewPager mPager;
    private ArrayList<Fragment> fragmentsList;
    private ImageView ivBottomLine;
    private TextView tvTabActivity, tvTabGroups, tvTabFriends;

    private int currIndex = 0;
    private int bottomLineWidth;
    private int offset = 0;
    private int position_one;
    private int position_two;
    
    private Resources resources;
	private ImageView iBack;
	private TextView iAdd,tTitle;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_my_goods_old);
        resources = getResources();
        
        final ActionBar actionBar = getActionBar();  
        if (actionBar!=null)
        {
	        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);  
	        actionBar.setCustomView(R.layout.actionbar);//�Զ���ActionBar����  
        
            tTitle = (TextView)findViewById(R.id.actionbar_title);
	        iBack = (ImageView)findViewById(R.id.left_btn);
	        iAdd = (TextView)findViewById(R.id.right_btn);
	        
	        tTitle.setText(resources.getText(R.string.my_goods));
	        
	        iBack.setOnClickListener(new OnClickListener()
	        {	
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
	         	    finish();
				}
	        	
	        });
	        
	        iAdd.setText(resources.getText(R.string.action_add));
	
	        iAdd.setOnClickListener(new OnClickListener()
	        {	
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent =new Intent();
					intent.setClass(MyGoodsActivityOld.this,SelectMethodActivity.class);
	         	    startActivity(intent);         	    
				}
	        	
	        });
        }
        InitWidth();
        InitTextView();
        InitViewPager();
    }

    private void InitTextView() {
        tvTabActivity = (TextView) findViewById(R.id.tv_tab_1);
        tvTabGroups = (TextView) findViewById(R.id.tv_tab_2);
        tvTabFriends = (TextView) findViewById(R.id.tv_tab_3);

        tvTabActivity.setOnClickListener(new MyOnClickListener(0));
        tvTabGroups.setOnClickListener(new MyOnClickListener(1));
        tvTabFriends.setOnClickListener(new MyOnClickListener(2));
    }

    private void InitViewPager() {
        mPager = (ViewPager) findViewById(R.id.vPager);
        fragmentsList = new ArrayList<Fragment>();                
       
        Fragment salingfragment = GoodsListFragment.newInstance(STYLE_ON_SHELF,null);
        Fragment offShelfFragment = GoodsListFragment.newInstance(STYLE_OFF_SHELF,null);
        Fragment stylesFragment= GoodsListFragment.newInstance(STYLE_GOODS_STYLES,null); 
        
        fragmentsList.add(salingfragment);
        fragmentsList.add(offShelfFragment);
        fragmentsList.add(stylesFragment);
        
        mPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList));
        mPager.setCurrentItem(0);
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());
    }

    private void InitWidth() {
        ivBottomLine = (ImageView) findViewById(R.id.iv_bottom_line);
        bottomLineWidth = ivBottomLine.getLayoutParams().width;
        Log.d(TAG, "cursor imageview width=" + bottomLineWidth);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;
        offset = (int) ((screenW / 3.0 - bottomLineWidth) / 2);
        Log.i("MyStoreActivity", "offset=" + offset);

        position_one = (int) (screenW / 3.0);
        position_two = position_one * 2;
    }

    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mPager.setCurrentItem(index);
        }
    };

    public class MyOnPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageSelected(int arg0) {
            Animation animation = null;
            switch (arg0) {
            case 0:
                if (currIndex == 1) {
                    animation = new TranslateAnimation(position_one, 0, 0, 0);
                    tvTabGroups.setTextColor(resources.getColor(R.color.black));
                } else if (currIndex == 2) {
                    animation = new TranslateAnimation(position_two, 0, 0, 0);
                    tvTabFriends.setTextColor(resources.getColor(R.color.black));
                }
                tvTabActivity.setTextColor(resources.getColor(R.color.red));
                break;
            case 1:
                if (currIndex == 0) {
                    animation = new TranslateAnimation(0, position_one, 0, 0);
                    tvTabActivity.setTextColor(resources.getColor(R.color.black));
                } else if (currIndex == 2) {
                    animation = new TranslateAnimation(position_two, position_one, 0, 0);
                    tvTabFriends.setTextColor(resources.getColor(R.color.black));
                } 
                tvTabGroups.setTextColor(resources.getColor(R.color.red));
                break;
            case 2:
                if (currIndex == 0) {
                    animation = new TranslateAnimation(0, position_two, 0, 0);
                    tvTabActivity.setTextColor(resources.getColor(R.color.black));
                } else if (currIndex == 1) {
                    animation = new TranslateAnimation(position_one, position_two, 0, 0);
                    tvTabGroups.setTextColor(resources.getColor(R.color.black));
                }
                tvTabFriends.setTextColor(resources.getColor(R.color.red));
                break;
/*            case 3:
                if (currIndex == 0) {
                    animation = new TranslateAnimation(0, position_three, 0, 0);
                    tvTabActivity.setTextColor(resources.getColor(R.color.lightwhite));
                } else if (currIndex == 1) {
                    animation = new TranslateAnimation(position_one, position_three, 0, 0);
                    tvTabGroups.setTextColor(resources.getColor(R.color.lightwhite));
                } else if (currIndex == 2) {
                    animation = new TranslateAnimation(position_two, position_three, 0, 0);
                    tvTabFriends.setTextColor(resources.getColor(R.color.lightwhite));
                }
                tvTabChat.setTextColor(resources.getColor(R.color.white));
                break;*/
            }
            currIndex = arg0;
            animation.setFillAfter(true);
            animation.setDuration(300);
            ivBottomLine.startAnimation(animation);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }

	@Override
	public void onRefresh(PullToRefreshBase<ListView> arg0) {
		// TODO Auto-generated method stub
		
	}
}
